$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    //console.log(url);
    var inventoryDiameter = "";

    //点击 返回 箭头
    $(".head .goBack").on("click", function () {
        window.location.href = "ropetest.html";
    });

    //搜索框 获取焦点
    $("#goods_id").focus();

    //根据扫描内容 得到 信息
    $(document).on("keydown", "#goods_id", function (event) {
        if (event.keyCode == "13") {
            var val = $(this).val();
            $("#goods_id").val("");
            if (!val) {
                $alert("请扫描合绳机台！");
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            //加载提示符 显示
            loading_show("搜索中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/ropetest/getDateByMachineForFirst',
                data: {
                    machine: val
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    if (res.success) {
                        var data = res.result;
                        //加载提示符 隐藏
                        loading_hide();
                        // console.log(data);
                        $alert("扫描成功！");
                        //填充 页面
                        $(".work_list_li span._id").text(data._id);
                        $(".work_list_li span.ddh").text(data.saleorderNumber);
                        $(".work_list_li span.pcdh").text(data.workplanNumber);
                        $(".work_list_li span.khmc").text(data.companyNameShort);
                        $(".work_list_li span.sh_count").text(data.unitewireCount);
                        $(".work_list_li span.workplanNumber").text(data.workplanNumber);
                        $(".work_list_li span.machine").text(data.machine);
                        $(".work_list_li span.cpbz").text(data.workplan.standardtype);
                        $(".work_list_li span.cpmc").text(data.inventoryName);
                        $(".work_list_li span.cpgg").text(data.inventorySpecification);
                        $(".work_list_li span.ddmc").text(data.workplan.length);
                        $(".work_list_li span.tyfs").text(data.inventoryData.inventoryOil);
                        $(".work_list_li span.sxpx").text(data.takeupWay.wheel);
                        $(".work_list_li span.up_sj").text(res.upData ? res.upData.firstInspectionTime : "");
                        $(".work_list_li span.up_jl").text(res.upData ? res.upData.laboratoryResult : "");
                        $(".work_list_li span.up_bhgx").text(res.upData ? res.upData.laboratoryUnqualifiedItems : "");
                        inventoryDiameter = data.inventoryDiameter;
                    } else {
                        //加载提示符 隐藏
                        loading_hide();
                        console.log(res);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    //加载提示符 隐藏
                    loading_hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            });
        }
    });

    // 不圆度(%)=(A1-A2)/公称直径
    $(".actualDiameter, .actualDiameterA2").on("change", function () {
        var actualDiameter = $(".content .actualDiameter").val(); // A1
        var actualDiameterA2 = $(".content .actualDiameterA2").val(); // A2
        if (actualDiameter && actualDiameterA2) {
            var ovality = (Math.abs(actualDiameter - actualDiameterA2) / inventoryDiameter * 100).toFixed(2)
            console.log("不圆度：" + ovality)
            $(".content .ovality").val(ovality)
        }
    });

    // 直径均匀性偏差(%)=[AX(A1,A2,B1,B2)一MIN(A1,A2,B1,B2刃/公称直径
    $(".actualDiameter, .actualDiameterA2, .actualDiameterB1, .actualDiameterB2").on("change", function () {
        var actualDiameter = $(".content .actualDiameter").val(); // A1
        var actualDiameterA2 = $(".content .actualDiameterA2").val(); // A2
        var actualDiameterB1 = $(".content .actualDiameterB1").val(); // B1
        var actualDiameterB2 = $(".content .actualDiameterB2").val(); // B2
        if (actualDiameter && actualDiameterA2 && actualDiameterB1 && actualDiameterB2) {
            var minValue = Math.min(actualDiameter, actualDiameterA2, actualDiameterB1, actualDiameterB2)
            var maxValue = Math.max(actualDiameter, actualDiameterA2, actualDiameterB1, actualDiameterB2)
            var diameterUniformityDeviation = ((maxValue - minValue) / inventoryDiameter * 100).toFixed(2)
            console.log("直径均匀性偏差：" + diameterUniformityDeviation)
            $(".content .diameterUniformityDeviation").val(diameterUniformityDeviation)
        }
    });
    // 平均直径偏差(%)= Math.abs(AVERAGE(A1,A2)-AVERAGE(B1,B2))/公称直径
    $(".actualDiameter, .actualDiameterA2, .actualDiameterB1, .actualDiameterB2").on("change", function () {
        var actualDiameter = $(".content .actualDiameter").val(); // A1
        var actualDiameterA2 = $(".content .actualDiameterA2").val(); // A2
        var actualDiameterB1 = $(".content .actualDiameterB1").val(); // B1
        var actualDiameterB2 = $(".content .actualDiameterB2").val(); // B2
        if (actualDiameter && actualDiameterA2 && actualDiameterB1 && actualDiameterB2) {
            var A = accDiv(accAdd(actualDiameter, actualDiameterA2), 2);
            var B = accDiv(accAdd(actualDiameterB1, actualDiameterB2), 2);
            var diameterAverageDeviation = (Math.abs(A - B) / inventoryDiameter * 100).toFixed(2)
            console.log("平均直径偏差：" + diameterAverageDeviation)
            $(".content .diameterAverageDeviation").val(diameterAverageDeviation)
        }
    });

    //点击 确认
    $(".foot .add_receive").on("click", function () {
        if (pass == 0) {
            return
        }
        pass = 0;
        var laboratoryResult = $("select.laboratoryResult").val();
        var reason = $(".content .reason").val();
        var select_sjAgreeProductionWorker = $(".content select.sjAgreeProductionWorker").val();
        var input_sjAgreeProductionWorker = $(".content input.sjAgreeProductionWorker").val();
        var sjAgreeProductionWorker = select_sjAgreeProductionWorker ? select_sjAgreeProductionWorker : input_sjAgreeProductionWorker;
        if (!laboratoryResult) {
            pass = 1;
            return $alert("判定结果不可为空");
        }
        if (laboratoryResult == "不合格" && !reason) {
            pass = 1;
            return $alert("不合格项目不可为空");
        }
        if (select_sjAgreeProductionWorker && input_sjAgreeProductionWorker) {
            pass = 1;
            return $alert("不可同时填写：同意生产人员");
        }
        if (laboratoryResult == "合格") {
            reason = "";
        }
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "POST",
            url: url + "/app/ropetest/ropeFirstInspectionRecord",
            data: {
                machine: $(".work_list_li span.machine").text(),
                checkTheNumberOfCords: $(".work_list_li span.sh_count").text(),
                conformityStrandRopeStruct: $("select.conformityStrandRopeStruct").val() || "",
                conformityLengthSet: $("select.conformityLengthSet").val() || "",
                directionOfTwist: $("select.directionOfTwist").val() || "",
                twistQuality: $("select.twistQuality").val() || "",
                unlooseness: $("select.unlooseness").val() || "",
                degreeOfFlatness: $("select.degreeOfFlatness").val() || "",
                surfaceQuality: $("select.surfaceQuality").val() || "",
                oilQuality: $("select.oilQuality").val() || "",
                conformitySizingRoll: $("select.conformitySizingRoll").val() || "",
                conformityLineTile: $("select.conformityLineTile").val() || "",
                actualDiameter: $("input.actualDiameter").val() || "",
                actualDiameterA2: $("input.actualDiameterA2").val() || "",
                actualDiameterB1: $("input.actualDiameterB1").val() || "",
                actualDiameterB2: $("input.actualDiameterB2").val() || "",
                actualPitchOfTwist: $("input.actualPitchOfTwist").val() || "",
                diameterUniformityDeviation: $("input.diameterUniformityDeviation").val() || "",
                diameterAverageDeviation: $("input.diameterAverageDeviation").val() || "",
                oilType: $("input.oilType").val() || "",
                ovality: $("input.ovality").val() || "",
                residualTorsion: $("input.residualTorsion").val() || "",
                deformationRateOfEachStrandMax: $("input.deformationRateOfEachStrandMax").val() || "",
                deformationRateOfEachStrandMin: $("input.deformationRateOfEachStrandMin").val() || "",
                laboratoryResult: laboratoryResult,
                reason: reason,
                sjInspectionConclusion: $("select.sjInspectionConclusion").val() || "",
                sjAgreeProductionWorker: sjAgreeProductionWorker,
                comment: $("input.comment").val() || "",
            },
            success: function (res) {
                pass = 1;
                loading_hide();
                if (res.success) {
                    loading_hide();
                    $alert($(".work_list_li span.machine").text() + "首检成功");
                    setTimeout(function () {
                        window.location.href = "ropetest_first.html";
                    }, 1400);
                } else {
                    console.log(res);
                    $alert(res.message);
                }
            },
            error: function (error) {
                pass = 1;
                loading_hide();
                console.log(error);
                $alert(error.responseText);
            }
        });
    });

    //点击 取消
    $(".foot .del_receive").on("click", function () {
        window.location.href = "ropetest.html";
    });

    // 获取 首检 不合格项目
    $.ajax({
        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
        type: "POST",
        url: url + "/app/ropetest/getRopeCheckUnqualifiedItemsBase",
        data: {
            type: "首检"
        },
        success: function (res) {
            pass = 1;
            loading_hide();
            if (res.success) {
                var data = res.result;
                $(".div_inventory .div_inventory_ul").empty();
                var inventory_html = "";
                for (var i = 0; i < data.length; i++) {
                    inventory_html += '<li><label class="label-checkbox item-content">' +
                        '<input type="checkbox" name="checkbox">' +
                        '<div class="item-media"><i class="icon icon-form-checkbox"></i></div>' +
                        '<div class="item-inner"><div class="item-title-row">' +
                        '<div class="item-title">' + data[i].unqualifiedItem + '</div></div></div></label></li>';
                }
                $(".div_inventory .div_inventory_ul").html(inventory_html);
            } else {
                console.log(res);
                $alert(res.message);
            }
        },
        error: function (error) {
            pass = 1;
            loading_hide();
            console.log(error);
            $alert(error.responseText);
        }
    });

    $(".li_reason").on("click", function () {
        var laboratoryResult = $("select.laboratoryResult").val();
        if (laboratoryResult == "不合格") {
            $.popup('.popup-about');
        } else {
            $alert("判定结果不是不合格")
        }
    });

    //关闭
    $(".div_inventory .icon-guanbi1").on("click", function () {
        $.closeModal('.popup-about');
    });
    //取消
    $(".div_inventory .quxiao").on("click", function () {
        $.closeModal('.popup-about');
    });

    //确认
    $(".div_inventory .queding").on("click", function () {
        var len = $(".div_inventory .div_inventory_ul input[name=checkbox]:checked").length;
        if (len == 0) {
            $alert("请最少选择一个不合格项目");
        }
        var text = [];
        for (var i = 0; i < len; i++) {
            var one = $(".div_inventory .div_inventory_ul input[name=checkbox]:checked").eq(i).parent().find(".item-title").text();
            text.push(one)
        }
        console.log(text)
        $.closeModal('.popup-about');
        $(".content .reason").val(text.join())
    });


});
